﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;


using groupAnalyzer.Observers;
using groupAnalyzer.model;

namespace groupAnalyzer
{
    /// <summary>
    /// Interaction logic for Searcher.xaml
    /// </summary>
    public partial class Searcher : Window, ObserverClient
    {

        private ViewFactory viewFactory;
        private Factory factory;
        private Observer observer;

        public void notify(Messages message, Object payLoad)
        {
            switch (message)
            {
                case Messages.search:
                    
                    break;

                case Messages.searched:
                    this.Dispatcher.Invoke(DispatcherPriority.Normal,
                    new Action(this.refreshBinding));
                    break;

                default:
                    Console.WriteLine("unimplemented message received");
                    break;
            }

        }

        public void refreshBinding()
        {
            this.searchResult.ItemsSource = this.viewFactory.Groups;
        }

        public Searcher(ViewFactory viewFactory, Observer observer)
        {
            this.viewFactory = viewFactory;
            this.observer = observer;
            InitializeComponent();
        }

        private void analyze_Clicked(object sender, RoutedEventArgs e)
        {
            bool pathSelected = (this.searchResult.SelectedIndex!=-1);
            string[] analyzeModes = { "member", "memberof" };
            // check if group is selected
            if (pathSelected)
            {
                string objectDn = ((GroupView)(this.searchResult.SelectedItem)).FQDN;
                List<object> payLoad = new List<object>();
                payLoad.Add(objectDn);
                payLoad.Add(
                    analyzeModes[this.analyzeMode.SelectedIndex]
                    );
                Observer.getGlobalObserver().notifiy(Messages.analyze, payLoad);
                this.Close();
            }
        }

        private void search_Clicked(object sender, RoutedEventArgs e)
        {
            this.observer.notifiy(Messages.search, this.searchText.Text);
        }

        private void browse_Click(object sender, RoutedEventArgs e)
        {
            // open tree field
        }

        private void clear_Clicked(object sender, RoutedEventArgs e)
        {
            
            this.searchResult.ItemsSource = this.viewFactory.Groups;
        }

        public static void init(ViewFactory viewFactory, Observer observer)
        {
            Searcher searcher = new Searcher(viewFactory,observer);
            observer.register(searcher);
            searcher.ShowDialog();
        }
    }
}
